package org.dangran.util;

import cn.hutool.core.codec.Base64Decoder;
import cn.hutool.core.codec.Base64Encoder;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.HexUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.crypto.symmetric.SymmetricAlgorithm;
import cn.hutool.crypto.symmetric.SymmetricCrypto;

public class Encryption {
    /**
    * 加密、解密
    * */
    public static void main(String[] args) {
        String content = "test中文";

        //hutool工具包--AES对称加解密
        //随机生成密钥
        byte[] key = SecureUtil.generateKey(SymmetricAlgorithm.AES.getValue()).getEncoded();
        String keyStr = Base64Encoder.encode(key);//密钥字符串
        //构建
        byte[] keyBt = Base64Decoder.decode(keyStr);
        SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, keyBt);
        //加密
        byte[] encrypt = aes.encrypt(content);
        //解密
        byte[] decrypt = aes.decrypt(encrypt);
        //加密为16进制表示
        String encryptHex = aes.encryptHex(content);
        //解密为字符串
        String decryptStr = aes.decryptStr(encryptHex, CharsetUtil.CHARSET_UTF_8);
        System.out.println(keyStr+"---密钥字符串\n"+encryptHex+"---加密为16进制表示\n"+decryptStr+"---解密为字符串");


        //hutool工具包--RSA非对称加解密
        RSA rsa = new RSA("","");
        byte[] aByte = HexUtil.decodeHex("aaa");
    }


}
